
// <copyright file="PerfCounter.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The perf counter.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Diagnostics.Performance
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// The perf counter.
    /// </summary>
    [Serializable]
    public sealed class PerfCounter
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        [NonSerialized]
        private PerformanceCounter adaptedObject;

        /// <summary>
        /// The raw value.
        /// </summary>
        private double _rawValue;

        #endregion Fields

        #region Public Properties

        /// <summary>
        /// Gets or sets the raw value.
        /// </summary>
        public double RawValue
        {
            get
            {
                return this._rawValue;
            }

            set
            {
                this._rawValue = value;
                if (this.adaptedObject != null)
                {
                    this.adaptedObject.RawValue = (long)value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the tag.
        /// </summary>
        public object Tag { get; set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The dispose.
        /// </summary>
        public void Dispose()
        {
            if (this.adaptedObject != null)
            {
                this.adaptedObject.RemoveInstance();
                this.adaptedObject.Dispose();
            }
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The set adaptedObject.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        internal void SetAdaptedObject(PerformanceCounter adaptedObject)
        {
            this.adaptedObject = adaptedObject;
        }

        #endregion Methods
    }
}
